<?php
/**
 * 会员管理
 * @author hushuai
 *
 */
class CustomAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 登记新会员
     * @access public
     * @param int $company   商户id
     * @param int $outletid  门店id
     * @param string $mobile 手机号码
     * @param string $vipno  会员卡号
     * @return array
     */
    public function add(){
    	
    	$email = $_POST['username'];
    	$one   = M('Member')->where("username='".$email."'")->find();
    	
    	if(!$one['id']){
    		$param = array(
    				"before"   => "member_add_before",
    		);
    		parent::do_add("Member",$param);
    	}else{
    		$this->error("该会员账号已存在");
    	}
    }
    
    /**
     * 会员冻结/解冻
     * @access public
     * @param varchar $id        会员id
     * @param varchar $status    状态
     */
    public function frost(){
    
    	$idStr  = $this->_req("id");
    	$status = $this->_req("status");
    	
    	if($idStr){
    		$idArr = explode(',',$idStr);
    		$map['status'] = $status;
    		foreach($idArr as $k=>$v){
    			$map['id'] = $v;
    			$view = M("Member")->save($map);
    		}
    		$this->success($view);
    	}else{
    		$this->error("参数非法，请重试");
    	}
    }
    
    /**
     * 会员卡读卡
     * @access public
     * @param  String vipno 会员卡号/手机号码
     * @return Int    id
     */
    public function readCard(){
    	
    	$vipno = $this->_get("vipno","trim","");
		
    	$data = D("Custom")->readCard($vipno);
    	
    	if($data['id']){
    		$this->success($data['id']);
    	}else{
    		$this->error("卡号或者手机号码不存在，请重试！");
    	}
    }
    /**
     * 返回会员信息
     * @access public
     * @param  String vipno 会员卡号/手机号码
     * @return array
     */
    public function readCards(){
    	 
    	$vipno = $this->_get("vipno","trim","");
    
    	$data = D("Custom")->readCard($vipno);
    	 
    	if($data['id']){
    		$this->success($data);
    	}else{
    		$this->error("卡号或者手机号码不存在，请重试！");
    	}
    }
    
    /**
     * 会员列表
     * @access public
     * @param int $company   商户id
     * @param int $outletid  门店id
     * @param string $where  查询条件
     * @return array
     */
    public function index(){
		
		$table    = "Member";
		
		$cat['city_id']   = $this->_req("city_id","intval",0);
    	//$cat['addid']     = $this->_req("addid","intval",0);
    	//$cat['role_view'] = $this->_req("role_view","intval",0);
    	//$cat['time_view'] = $this->_req("time_view","intval",0);
		
		//$map = D($table)->getRoleMember($cat);

    	$where    = $this->_req("where");
     	$company  = $this->_get("company",         "intval",  0);
    	$outletid = $this->_get("outletid",        "intval" , 0); 
    	$desc     = $this->_get("orderDirection",  "trim",    "desc");
		$order    = $this->_get("orderName",       "trim",    "addtime");
		$pageNum  = $this->_get("pageNum",         "intval",  C('PAGE_NUMS'));
		$nowPage  = $this->_get("nowPage",         "intval",  1);
		
     	if($where){
    		foreach($where as $k=>$v){
    			if($v){
	    			if($k=="keywords"){
	    				$map['mobile|username|email'] = array("like","%".$v."%");
	    			}else{
	    				$map[$k] = $v;
	    				
	    			}
    			}
    		}
    	}
    	
    	if($cat['city_id']){
    		$map['city']  = $cat['city_id'];
    	}
    	
    	$map['isdel'] = 2;
    	$this->success(parent::pageSelect($table,$map,$pageNum,$nowPage,$order.' '.$desc));
    }
	
	//会员列表无分页
	public function select(){
		
		$keywords = $this->_req('keywords');
		
		if($keywords){
			$map['mobile|email'] = array('like','%'.$keywords.'%');
		}
		$map['isdel']  = 2;
		$map['status'] = 1;
		
		$model = M('Member');
		$data  = $model->where($map)->select();
		$this->success($data);	
	}
 
    /**
     * 分页会员查询
     * @access public
     * @param string $desc     降序排列
     * @param string $order    排序方式
     * @param string $pageNum  每页数量
     * @param string $nowPage  当前页数
     * @return array
     */
    
    public function customSel(){
    	 
    	$desc     = $this->_get("orderDirection","trim","desc");;
    	$order    = $this->_get("orderName","trim","addtime");
    	$pageNum  = $this->_get("pageNum","intval",C('PAGE_NUMS'));
    	$nowPage  = $this->_get("p","intval",1);
    	$con      = $order.' '.$desc;
    	$table    = "Custom";
    	$map['isdel']   = 2;
    	 
    	$this->success($this->pageSelect($table,$map,$pageNum,$nowPage,$con));
    }
    
    
    /**
     * 修改会员信息
     * @access public
     * @param int $company   商户id
     * @param int $outletid  门店id
     * @param int $customid  会员信息id
     * @return array
     */
    public function edit(){

    	$param = array(
    		"before"   => "member_add_before",
    	);
    	parent::do_edit("Member",$param);
    }
    
    /**
     * 查看会员信息
     * @access public
     * @param int $company   商户id
     * @param int $outletid  门店id
     * @param int $customid  会员信息id
     * @param string $vipno  会员卡号
     * @return array
     */
    public function view(){
    
    	//$map['company']  = $this->_get("company",  "intval", 0);
    	//$map['outletid'] = $this->_get("outletid", "intval", 0);
    	$memberid        = $this->_get("memberid", "intval", 0);
    	
    	if($memberid){
    		$map['id'] = $memberid;
    	}
    	$model = M("Member");
    	$data  = $model->where($map)->find();
    	$data['password'] = myencrypt($data['password']);
    	
    	if($data['id']){
    		$this->success($data);
    	}else{
    		$this->error("会员信息查询失败，请稍后重试");
    	}
    }
    
    /**
     * 更换门店
     * @access public
     * @param int $company   商户id
     * @param int $newid     新门店id
     * @param int $customid  会员信息id
     * @param String $vipno  会员卡号
     * @return array
     */
    public function changeOutlet(){
    
    	$customid = $this->_get("customid","intval",0);
    	$company  = $this->_get("company","intval",0);
    	$newid    = $this->_get("new_outletid","intval",0);
    	$vipno    = $this->_get("vipno","trim","");
    	
    	$data = D("Custom")->changeOutlet($customid, $newid,$company);
    	$data = D("VipUser")->changeOutlet($customid,$newid,$vipno,$company);
    	
    	$this->success($data);
    }
    
    /**
     * 会员会员卡信息
     * @access public
     * @param  int $customid   会员信息id
     * @return array
     */
    public function upgrade_use(){
    
    	$map['customid'] = $this->_get("customid","intval",0);
    	$model           = D("VipcardVipcardUserView");
    	$models          = D("Vipcard");
    	$list['data']    = $model->where($map)->find();
    	$list['levels']  = $models->select();
    	$this->success($list);
    }
    
    /**
     * 会员卡升级
     */
    public function upgrade(){
    	
    	parent::do_edit("VipcardUser");
    }
    
    /**
     * 删除会员(逻辑删除)
     */
    public function del(){
    	
/*     	$map['company']  = $this->_get("company","intval",0);
    	$map['outletid'] = $this->_get("outletid","intval",0); */
    	 
    	$idStr = $this->_get("id","trim",'');
    	
    	if($idStr){
    		
    		$idArr = explode(',',$idStr);
    		
    		foreach($idArr as $k=>$v){
    			$map['id']    = $v;
    			$map['isdel'] = 1;
    			$data = M('Member')->save($map);
    		}
    	}
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("请选择您要删除的会员");
    	}
    }
}